<?php
/**
 * CoreFile
 *
 * @package File.class.php
 * @version $id$
 * @copyright blankyao
 * @author blankyao <blankyao@gmail.com>
 * @license  GNU General Public License 2.0
 * @todo 目录的相关操作,文件指针的操作
 */
class CoreFile
{
    /**
     * isFileExists
     * 文件是否存在
     * @var bool
     * @access protected
     */
    var $isFileExists = TRUE;
    /**
     * fileName
     *
     * @var string 文件名字
     * @access public
     */
    var $fileName;
    /**
     * isReadable
     * 文件是否可读
     * @var bool
     * @access public
     */
    var $isReadable = FALSE;
    /**
     * isWritable
     * 文件是否可写
     * @var bool
     * @access public
     */
    var $isWritable =FALSE;
    /**
     * fileExtension
     *
     * @var string 文件类型
     * @access public
     */
    var $fileExtension;
    /**
     * fileRealPath
     *
     * @var string 文件的绝对路径
     * @access public
     */
    var $fileRealPath;
    /**
     * isDir
     * 是否是一个目录
     * @var bool
     * @access public
     */
    var $isDir = false;
    /**
     * fileSize
     *
     * @var mixed 文件大小
     * @access public
     */
    var $fileSize;
    /**
     * fileMode
     *
     * @var numberic 文件属性
     * @access public
     */
    var $filePermission = 0755;
    /**
     * fileType
     *
     * @var string 文件的MIME类型
     * @access public
     */
    var $fileType;
    /**
     * _fileHandler
     *
     * @var resource 文件操作句柄
     * @access protected
     */
    var $_fileHandler;

    /**
     * CoreFile
     * 类构造函数
     * @param string $_file文件名字
     * @access public
     * @return void
     */
    function CoreFile($_file) {
        if(trim($_file)) {
            $this->fileName = $_file;
            if(!file_exists($this->fileName)) {
                $this->isFileExists = FALSE;
            } else {
                $this->fileRealPath = realpath($this->fileName);
                $this->isDir = is_dir($this->fileName);
                $this->isReadable = is_readable($this->fileName);
                $this->isWritable = is_writable($this->fileName);
                $this->_GetFileExtension();
                $this->_GetFileSize();
                $this->_GetFileType();
            }
        } else {
            $_error = "File name is invalid";
            throw new Core_Exception($_error);
        }
    }

    /**
     * _GetFileExtension
     * 取得文件的后缀
     * @access protected
     * @return void
     */
    function _GetFileExtension() {
        $this->fileExtension = strrchr( $this->fileName, "." );
    }

    /**
     * _GetFileSize
     * 取得文件的大小
     * @access protected
     * @return void
     */
    function _GetFileSize() {
        $_filesize = filesize($this->fileName);
        if($_filesize >= 1073741824) {
            $_filesize = round($_filesize / 1073741824 * 100) / 100 . ' G';
        } elseif($_filesize >= 1048576) {
            $_filesize = round($_filesize / 1048576 * 100) / 100 . ' M';
        } elseif($filesize >= 1024) {
            $_filesize = round($_filesize / 1024 * 100) / 100 . ' K';
        } else {
            $_filesize = $_filesize . ' bytes';
        }
        $this->fileSize = $_filesize;
    }
    /**
     * _GetFileType
     * 取得文件的mime类型
     * @access protected
     * @return void
     */
    function _GetFileType() {
        $_mimeTypes = array(
			".ai" => "application/postscript",
			".aif" => "audio/x-aiff",
			".asc" => "text/plain",
			".asf" => "video/x-ms-asf",
			".au" => "audio/basic",
			".avi" => "video/x-msvideo",
			".awf" => "application/vnd.adobe.workflow",
			".bcpio" => "application/x-bcpio",
			".bmp" => "application/x-bmp",
			".bot" => "application/x-bot",
			".bz2" => "application/x-bzip",
			".c4t" => "application/x-c4t",
			".c90" => "application/x-c90",
			".cal" => "application/x-cals",
			".cat" => "application/vnd.ms-pki.seccat",
			".cdr" => "application/x-cdr",
			".cel" => "application/x-cel",
			".cer" => "application/x-x509-ca-cert",
			".cg4" => "application/x-g4",
			".cgm" => "application/x-cgm",
			".cit" => "application/x-cit",
			".class" => "java/*",
			".cmp" => "application/x-cmp",
			".cmx" => "application/x-cmx",
			".cot" => "application/x-cot",
			".cpio" => "application/x-cpio",
			".cpt" => "application/mac-compactpro",
			".crl" => "application/pkix-crl",
			".csh" => "application/x-csh",
			".csi" => "application/x-csi",
			".css" => "text/css",
			".cut" => "application/x-cut",
			".dbf" => "application/x-dbf",
			".dbm" => "application/x-dbm",
			".dbx" => "application/x-dbx",
			".dcr" => "application/x-director",
			".dcx" => "application/x-dcx",
			".dgn" => "application/x-dgn",
			".dib" => "application/x-dib",
			".djvu" => "image/vnd.djvu",
			".dll" => "application/x-msdownload",
			".doc" => "application/msword",
			".drw" => "application/x-drw",
			".dvi" => "application/x-dvi",
			".dwf" => "application/x-dwf",
			".dwg" => "application/x-dwg",
			".dxb" => "application/x-dxb",
			".dxf" => "application/x-dxf",
			".edn" => "application/vnd.adobe.edn",
			".emf" => "application/x-emf",
			".eml" => "message/rfc822",
			".epi" => "application/x-epi",
			".eps" => "application/postscript",
			".etd" => "application/x-ebx",
			".etx" => "text/x-setext",
			".ez" => "application/andrew-inset",
			".fax" => "image/fax",
			".fdf" => "application/vnd.fdf",
			".fif" => "application/fractals",
			".frm" => "application/x-frm",
			".gbr" => "application/x-gbr",
			".gcd" => "application/x-gcd",
			".gif" => "image/gif",
			".gl2" => "application/x-gl2",
			".gp4" => "application/x-gp4",
			".gtar" => "application/x-gtar",
			".gz" => "application/x-gzip",
			".hdf" => "application/x-hdf",
			".hgl" => "application/x-hgl",
			".hlp" => "application/mshelp",
			".hmr" => "application/x-hmr",
			".hpg" => "application/x-hpgl",
			".hpl" => "application/x-hpl",
			".hqx" => "application/mac-binhex40",
			".hrf" => "application/x-hrf",
			".hta" => "application/hta",
			".htc" => "text/x-component",
			".htt" => "text/webviewhtml",
			".icb" => "application/x-icb",
			".ice" => "x-conference-xcooltalk",
			".ico" => "application/x-ico",
			".ief" => "image/ief",
			".iff" => "application/x-iff",
			".iges" => "model/iges",
			".igs" => "application/x-igs",
			".iii" => "application/x-iphone",
			".img" => "application/x-img",
			".ins" => "application/x-internet-signup",
			".ivf" => "video/x-ivf",
			".jpeg" => "image/jpeg ",
			".jpg" => "image/jpeg",
			".js" => "text/javascript",
			".kar" => "audio/midi",
			".la1" => "audio/x-liquid-file",
			".lar" => "application/x-laplayer-reg",
			".latex" => "application/x-latex",
			".lavs" => "audio/x-liquid-secure",
			".lbm" => "application/x-lbm",
			".lmsff" => "audio/x-la-lms",
			".ls" => "application/x-javascript",
			".ltr" => "application/x-ltr",
			".m1v" => "video/x-mpeg",
			".m3u" => "audio/mpegurl",
			".m4e" => "video/mpeg4",
			".mac" => "application/x-mac",
			".man" => "application/x-troff-man",
			".mdb" => "application/x-mdb",
			".me" => "application/x-troff-me",
			".mi" => "application/x-mi",
			".mid" => "audio/x-midi",
			".mil" => "application/x-mil",
			".mnd" => "audio/x-musicnet-download",
			".mns" => "audio/x-musicnet-stream",
			".mov" => "video/quicktime",
			".movie" => "video/x-sgi-movie",
			".mp1" => "audio/mp1",
			".mp2" => "audio/x-mpeg",
			".mp3" => "audio/mpeg",
			".mpa" => "video/x-mpg",
			".mpd" => "application/vnd.ms-project",
			".mpeg" => "video/mpeg ",
			".mpg" => "video/mpeg",
			".mpga" => "audio/rn-mpeg",
			".mpv" => "video/mpg",
			".ms" => "application/x-troff-ms",
			".msh" => "model/mesh",
			".mxp" => "application/x-mmxp",
			".mxu" => "video/vnd.mpegurl",
			".nc" => "application/x-netcdf",
			".net" => "image/pnetvue",
			".nrf" => "application/x-nrf",
			".oda" => "application/oda",
			".odc" => "text/x-ms-odc",
			".out" => "application/x-out",
			".p10" => "application/pkcs10",
			".p12" => "application/x-pkcs12",
			".p7b" => "application/x-pkcs7-certificates",
			".p7c" => "application/pkcs7-mime",
			".p7r" => "application/x-pkcs7-certreqresp",
			".p7s" => "application/pkcs7-signature",
			".pac" => "application/x-ns-proxy-autoconfig",
			".pbm" => "image/x-portable-bitmap",
			".pc5" => "application/x-pc5",
			".pci" => "application/x-pci",
			".pcl" => "application/x-pcl",
			".pcx" => "application/x-pcx",
			".pdb" => "chemical/x-pdb",
			".pdf" => "application/pdf",
			".pdx" => "application/vnd.adobe.pdx",
			".pgl" => "application/x-pgl",
			".pgm" => "image/x-portable-graymap",
			".pgn" => "application/x-chess-pgn",
			".php" => "application/x-httpd-php",
			".pic" => "application/x-pic",
			".pko" => "application/vnd.ms-pki.pko",
			".pl" => "application/x-perl",
			".pls" => "audio/scpls",
			".plt" => "application/x-plt",
			".png" => "application/x-png",
			".pnm" => "image/x-portable-anymap",
			".pot" => "application/mspowerpoint",
			".ppa" => "application/vnd.ms-powerpoint",
			".ppm" => "application/x-ppm",
			".ppt" => "application/mspowerpoint ",
			".pr" => "application/x-pr",
			".prf" => "application/pics-rules",
			".prn" => "application/x-prn",
			".prt" => "application/x-prt",
			".ptn" => "application/x-ptn",
			".qt" => "video/quicktime ",
			".r3t" => "text/vnd.rn-realtext3d",
			".ra" => "audio/vnd.rn-realaudio",
			".ram" => "application/vnd.rn-realmedia",
			".rar" => "application/rar",
			".ras" => "application/x-ras",
			".rat" => "application/rat-file",
			".rec" => "application/vnd.rn-recording",
			".red" => "application/x-red",
			".rgb" => "application/x-rgb",
			".rjs" => "application/vnd.rn-realsystem-rjs",
			".rjt" => "application/vnd.rn-realsystem-rjt",
			".rlc" => "application/x-rlc",
			".rle" => "application/x-rle",
			".rmf" => "application/vnd.adobe.rmf",
			".rmi" => "audio/mid",
			".rmj" => "application/vnd.rn-realsystem-rmj",
			".rmm" => "audio/x-pn-realaudio",
			".rmp" => "application/vnd.rn-rn_music_package",
			".rms" => "application/vnd.rn-realmedia-secure",
			".rmvb" => "video/vnd.rn-realvideo",
			".rmx" => "application/vnd.rn-realsystem-rmx",
			".rnx" => "application/vnd.rn-realplayer",
			".rp" => "image/vnd.rn-realpix",
			".rpm" => "audio/x-pn-realaudio-plugin",
			".rsml" => "application/vnd.rn-rsml",
			".rt" => "text/vnd.rn-realtext",
			".rtf" => "application/rtf",
			".rtx" => "text/richtext",
			".sam" => "application/x-sam",
			".sat" => "application/x-sat",
			".sdp" => "application/sdp",
			".sdw" => "application/x-sdw",
			".sgml" => "text/sgml",
			".sh" => "application/x-sh",
			".shar" => "application/x-shar",
			".sig" => "application/pgp-signature",
			".sit" => "application/x-stuffit",
			".skp" => "application/x-koan",
			".slb" => "application/x-slb",
			".sld" => "application/x-sld",
			".slk" => "drawing/x-slk",
			".smi" => "application/smil",
			".smk" => "application/x-smk",
			".spl" => "application/futuresplash",
			".src" => "application/x-wais-source",
			".ssm" => "application/streamingmedia",
			".sst" => "application/vnd.ms-pki.certstore",
			".stl" => "application/vnd.ms-pki.stl",
			".sty" => "application/x-sty",
			".sv4cpio" => "application/x-sv4cpio",
			".sv4crc" => "application/x-sv4crc",
			".swf" => "application/x-shockwave-flash",
			".t" => "application/x-troff",
			".tar" => "application/x-tar",
			".tar.gz" => "application/x-tgz",
			".tbz" => "application/x-bzip-compressed-tar",
			".tcl" => "application/x-tcl",
			".tdf" => "application/x-tdf",
			".tex" => "application/x-tex",
			".texinfo" => "application/x-texinfo",
			".tg4" => "application/x-tg4",
			".tga" => "application/x-tga",
			".tif" => "application/x-tif",
			".tiff" => "image/tiff",
			".top" => "drawing/x-top",
			".torrent" => "application/x-bittorrent",
			".tsv" => "text/tab-seperated-values",
			".uin" => "application/x-icq",
			".uls" => "text/iuls",
			".ustar" => "application/x-ustar",
			".vcd" => "application/x-cdlink",
			".vcf" => "text/x-vcard",
			".vda" => "application/x-vda",
			".vdx" => "application/vnd.visio",
			".viv" => "video/vnd.vivo",
			".vpg" => "application/x-vpeg005",
			".vsd" => "application/x-vsd",
			".vst" => "application/x-vst",
			".wav" => "audio/x-wav",
			".wax" => "audio/x-ms-wax",
			".wb1" => "application/x-wb1",
			".wb2" => "application/x-wb2",
			".wb3" => "application/x-wb3",
			".wbmp" => "image/vnd.wap.wbmp",
			".wbxml" => "application/vnd.wap.wbxml",
			".wk3" => "application/x-wk3",
			".wk4" => "application/x-wk4",
			".wkq" => "application/x-wkq",
			".wks" => "application/x-wks",
			".wm" => "video/x-ms-wm",
			".wma" => "audio/x-ms-wma",
			".wmd" => "application/x-ms-wmd",
			".wmf" => "application/x-wmf",
			".wml" => "text/vnd.wap.wml",
			".wmlc" => "application/vnd.wap.wmlc",
			".wmls" => "text/vnd.wap.wmlscript",
			".wmlsc" => "application/vnd.wap.wmlscriptc",
			".wmv" => "video/x-ms-wmv",
			".wmx" => "video/x-ms-wmx",
			".wmz" => "application/x-ms-wmz",
			".wp6" => "application/x-wp6",
			".wpd" => "application/x-wpd",
			".wpg" => "application/x-wpg",
			".wpl" => "application/vnd.ms-wpl",
			".wq1" => "application/x-wq1",
			".wr1" => "application/x-wr1",
			".wri" => "application/x-wri",
			".wrk" => "application/x-wrk",
			".wrl" => "model/vrml",
			".ws" => "application/x-ws",
			".wsc" => "text/scriptlet",
			".wvx" => "video/x-ms-wvx",
			".x_b" => "application/x-x_b",
			".x_t" => "application/x-x_t",
			".xbm" => "image/x-xbitmap",
			".xdp" => "application/vnd.adobe.xdp",
			".xfd" => "application/vnd.adobe.xfd",
			".xfdf" => "application/vnd.adobe.xfdf",
			".xht" => "application/xhtml+xml",
			".xhtml" => "text/html",
			".xla" => "application/msexcel",
			".xls" => "application/msexcel ",
			".xlw" => "application/x-xlw",
			".xml" => "text/xml",
			".xpm" => "image/x-xpixmap",
			".xwd" => "image/x-windowdump",
			".xyz" => "chemical/x-xyz",
			".zip" => "application/zip",
        );
        if (isset( $_mimeTypes[$this->extension] ) ) {
            $this->fileType = $_mimeTypes[$this->extension];
        } else {
            $this->fileType = 'application/octet-stream';
        }
    }
    /**
     * OpenFile
     * 打开文件获得文件句柄
     * @param mixed $_mode 打开模式
     * @access public
     * @return void
     */
    function OpenFile($_mode = "r") {
        if($this->_fileHandler) {
            if(!$this->isFileExists) {
                $this->_fileHandler = fopen($this->fileName, $_mode);
            } else {
                $_error = "The file " . $this->fileName . " is not readable";
                throw new Core_Exception($_error);
            }
        } else {
            $this->_fileHandler = fopen($this->fileName, $_mode);
        }
    }
    /**
     * DelFile
     * 删除文件
     * @access public
     * @return void
     */
    function DelFile() {
        if(!$this->isWritable || !@unlink($this->fileName)){
            $_error = "The file ". $this->fileName ."is not readable";
            throw new Core_Exception($_error);
        }
    }

    /**
     * RenameFile
     * 重命名文件
     * @access public
     * @return void
     */
    function RenameFile($_newName) {
        if($_newFile != $this->fileName) {
            if(@copy($this->fileName, $_newName)) {
                if(!@unlink($this->fileName)) {
                    $_error = "The file ". $this->fileName ."is not readable";
                    throw new Core_Exception($_error);     
                }
                $this->fileName = $_newName;
                return true;
            } else {
                $_error = "Copy filed for unkonw reason";
                throw new Core_Exception($_error); 
            }
        } else {
            return true;
        }
    }

    /**
     * ReadFile
     * 读取文件
     * @param numberic $_size
     * @access public
     * @return void
     */
    function ReadFile($_size = 1024) {
        if(!$this->isReadable) {
            $_error = "The file " .$this->fileName. " is not readable";
            throw new Core_Exception($_error);
        }
        if($this->_fileHandler) {
            return fread($this->_fileHandler, $_size);
        } else {
            $this->OpenFile();
            return $this->ReadFile();
        }
    }
    /**
     * ReadFileLine
     * 按行读取文件
     * @param numberic $_size
     * @access public
     * @return void
     */
    function ReadFileLine($_size) {
        if(!$this->isReadable) {
            $_error = "The file " . $this->fileName . " is not readable";
            throw new Core_Exception($_error);
        }
        if($this->_fileHandler) {
            return fgets($this->_fileHandler, $_size);
        } else {
            $this->OpenFile();
            return $this->ReadFile();
        }
    }
    /**
     * WriteFile
     * 写入文件
     * @param mixed $data
     * @access public
     * @return void
     */
    function WriteFile($_data) {
        if($this->_fileHandler) {
            if($this->isWritable) {
                if(fwrite($this->_fileHandler, $_data)) {
                    $this->SetPermission();
                } else {
                    $_error = "Write file <b>" . $this->fileName . " </b> failed for unkonw reason";
                    throw new Core_Exception($_error);
                }
            } else {
                $_error = "The file <b>" . $this->fileName . " </b> is not writable";
                throw new Core_Exception($_error);
            }
        } else {
            $this->OpenFile("w");
            $this->WriteFile($_data);
        }
    }
    /**
     * WriteParam
     * 将一个变量写入文件(可以为数组)
     * @param mixed $_param
     * @access public
     * @return void
     */
    function WriteParam($_param) {
    }
    /**
     * _DelDirs
     * 删除目录
     * @access public
     * @return void
     */
    /**
     * SetPermission
     *
     * @param numberic $_permission 要设置的权限
     * @access public
     * @return void
     */
    function SetPermission($_permission = 0755) {
        $this->filePermission = $_permission;
        if(!@chmod($this->fileName, $this->filePermission)) {
            $_error = "Change permission failed for file <b>" .$this->fileName. "</b>";
            throw new Core_Exception($_error);
        }
    }
    function DelDirs() {

    }
    /**
     * MakeDirs
     * 建立目录
     * @access public
     * @return void
     */
    function MakeDirs() {
    }
}
?>
